<?php

namespace app\home\controller;

use app\home\lib\WeiXin;
use think\Db;

class Order extends Base
{
    /*
    * 获取目前的支付方式
    * */
    public function getPayMethod()
    {
        $info = Db::name('pay_way')->select();
        return outputJson(0, 'ok', $info);
    }

    /*
    * 支付宝App支付下单
    * */
    public function aliPayApp()
    {
        $pay_info = Db::name('pay_way')->where(array('id' => $pay_id))->find();
        $order_info = array(
            'uid' => $this->id,
            'order_class' => $class,
            'add_time' => date('Y-m-d H:i:s'),
            'pay_way' => '1',
            'month' => isset($month) ? $month : 0,
            'money' => $money,
            'is_fcb' => $isFcb,
            'order_no' => $orderPre . '_' . date('YmdHis') . mt_rand(1000, 9999) . $this->uid,
            'remark' => '支付宝App支付',
        );
        $re = Db::name('order')->insert($order_info);
        if ($re) {
            $result = $this->orderAliPayApp($order_info['order_no'], $order_info['money'], $dec, $goodsDes, $pay_info);
            return outputJson(1000, lang('code_1000'), $result);
        } else {
            return outputJson(2002, lang('code_2002'));
        }
    }

    /*
     * 支付宝APP支付参数组装
     * */
    public function orderAliPayApp($order_no, $money, $goods_des, $sub, $config, $notify = '/axh/aliPayBack')
    {
        $order_info = [
            'order_no' => $order_no,
            'money' => $money,
            'goods_des' => $goods_des,
            'sub' => $sub,
            'notifyUrl' => (config('current_env') == 'formal' ? config('openapi.fuluNotifyUrl') : config('openapi.test_fuluNotifyUrl')) . $notify,
        ];
        return AliPayApp($order_info, $config);
    }

    public function wxPayApp()
    {
        if (input('post.')) {
            $class = input('post.class');//订单类型
            $pay_id = input('post.id');//支付帐号的id
            $pay_info = Db::name('pay_way')->where(array('id' => $pay_id))->find();
            $money = 0;

            $orderPre = "Yyd_";
            $redis = new Redis();
            if ($redis->get('APPSOURCE:' . $this->user_info['mobile']) == "youqianhua") {
                $orderPre = "Yqh_";
            } elseif ($redis->get('APPSOURCE:' . $this->user_info['mobile']) == "xinandai") {
                $orderPre = "Xad_";
            } elseif ($redis->get('APPSOURCE:' . $this->user_info['mobile']) == "youshanhua") {
                $orderPre = "Ysh_";
            }
            $order_info = array(
                'uid' => $this->uid,
                'order_class' => $class,
                'add_time' => date('Y-m-d H:i:s'),
                'pay_way' => '2',
                'month' => isset($month) ? $month : 0,
                'money' => $money,
                'is_fcb' => $isFcb,
                'order_no' => $orderPre . date('YmdHis') . mt_rand(1000, 9999) . $this->uid,
                'remark' => '微信App支付',
            );
            $is_debug = $this->is_debug;
            if ($is_debug == '2') {//代表是测试帐号
                $order_info['money'] = 0.01;
            } else {
                if ($money < 1) {
                    return outputJson(2001, lang('code_2001'));
                }
            }
            if ($isFcb == 1) {
                $order_info['money'] = $order_info['money'] + config('fan_cui_bang_now_price');
                $order_info['fcb_over_time'] = date('Y-m-d', time() + 31 * 24 * 3600);//设置反催帮的过期时间
            }
            $re = Db::name('order')->insert($order_info);
            if ($re) {

                $data['notify_url'] = (config('current_env') == 'formal' ? config('openapi.fuluNotifyUrl') : config('openapi.test_fuluNotifyUrl')) . "/axh/WxBack";//微信支付成功异步回调地址
                $data['body'] = $dec;//支付商品描述
                $data['attach'] = 'Yyd';//异步回调标记
                $data['out_trade_no'] = $order_info['order_no'];//支付订单号
                $data['total_fee'] = $money;//支付金额
                $data['mobile'] = $this->user_info['mobile'];//支付金额
                $temp = $this->wxpay($data);

                return outputJson(1000, lang('code_1000'), $temp);
            } else {
                return outputJson(2002, lang('code_2002'));
            }
        }

    }

    /*微信支付App*/
    public function wxPay($list)
    {
        $data['notify_url'] = $list['notify_url'];//微信支付成功异步回调地址
        $data['body'] = $list['body'];//支付商品描述
        $data['attach'] = $list['attach'];//异步回调标记
        $data['out_trade_no'] = $list['out_trade_no'];//支付订单号
        $data['total_fee'] = (float)$list['total_fee'];//支付金额
        $data['mobile'] = $list['mobile'];//支付金额
        vendor('WxPay_App.JsApi');
        $obj = new \JsApi();
        $result = $obj->wx_jsapi($data);
        return $result;
    }
}